namespace DigitalCv.Data
{
    using System;
    using System.IO;

    public class DigitalCvRespository : IDigitalCvRespository
    {
        private XamlSerializer xamlSerializer;

        public DigitalCvRespository()
        {
            this.xamlSerializer = new XamlSerializer();
        }

        public Cv NewDigitalCv()
        {
            var dateTimeNow = DateTime.Now;
            var digitalCv = new Cv
                {
                    CreatedDate = dateTimeNow,
                    LastModified = dateTimeNow
                };

            return digitalCv;
        }

        public Cv OpenDigitalCv(string filePath)
        {
            var xaml = File.ReadAllText(filePath);
            return this.xamlSerializer.Deserialize<Cv>(xaml);
        }

        public void SaveDigitalCv(Cv cv, string filePath)
        {
            var xaml = this.xamlSerializer.Serialize(cv);
            File.WriteAllText(filePath, xaml);
        }
    }
}